cmake_minimum_required(VERSION 3.15.2)

project(eazyptotobuf C CXX)

option(GLIBC_COMPATIBILITY "Enable compatibility with older glibc libraries." ON)
message(STATUS "GLIBC_COMPATIBILITY is ${GLIBC_COMPATIBILITY}")

# Set dirs
set(BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}")
#set(GENSRC_DIR "${BASE_DIR}/../gensrc/build/")
set(SRC_DIR "${BASE_DIR}/src/")
set(OUTPUT_DIR "${BASE_DIR}/output")


set(Protobuf_INCLUDE_DIR ${BASE_DIR}/installed/include)
set(Protobuf_LIBRARIES ${BASE_DIR}/installed/lib)

include_directories( ${Protobuf_INCLUDE_DIR} )
# add library directory
link_directories(${Protobuf_LIBRARIES})



find_package(Protobuf REQUIRED)
find_package(Threads REQUIRED)

 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -static-libstdc++ -static-libgcc")

# set CMAKE_BUILD_TYPE
if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE RELEASE)
endif()

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
message(STATUS "Build type is ${CMAKE_BUILD_TYPE}")

# set CMAKE_BUILD_TARGET_ARCH
# use `lscpu | grep 'Architecture' | awk '{print $2}'` only support system which language is en_US.UTF-8
execute_process(COMMAND bash "-c" "uname -m"
        OUTPUT_VARIABLE
        CMAKE_BUILD_TARGET_ARCH
        OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Build target arch is ${CMAKE_BUILD_TARGET_ARCH}")


execute_process(
    COMMAND bash -c "${PROJECT_SOURCE_DIR}/gensrc.sh "
    OUTPUT_VARIABLE GENSRC_RESULT
)

message("gensrc result ${GENSRC_RESULT}")





# Check gcc
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.3.0")
        message(FATAL_ERROR "Need GCC version at least 7.3.0")
    endif()
    message(STATUS "GCC version is greater than 7.3.0, disable -Werror. Be careful with compile warnings.")
elseif (NOT APPLE)
    message(FATAL_ERROR "Compiler should be GNU")
endif()



set( APP_SOURCES
        main.cpp
        addressbook.pb.cc)



add_executable(main ${APP_SOURCES})
target_link_libraries(main protoc libprotobuf.a)

install(TARGETS
        main
        DESTINATION ${OUTPUT_DIR})